﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Collections;

namespace cn.Util
{
    /// <summary>
    /// 调用WebApi
    /// </summary>
    public class WebApi
    {
        string strEncoding = "utf-8";

        string accept = "application/json";

        string method = "post";
        /// <summary>
        /// 
        /// </summary>
        public WebApi()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_encoding"></param>
        /// <param name="_accept"></param>
        /// <param name="_method"></param>
        public WebApi(string _encoding, string _accept,string _method)
        {
            strEncoding = _encoding;
            accept = _accept;
            method=_method;
        }

        public HttpMsg To(string url, string fromBody="", Dictionary<string, string> parameters=null)
        {
            Encoding encoding = System.Text.Encoding.GetEncoding(strEncoding);
            byte[] buffer = encoding.GetBytes(fromBody);
            if (buffer.Length != 0)
            {
                var d = new byte[] { 61 };
                d.CopyTo(buffer, 0);
            }
            return To(url , buffer, parameters);
        }

        public HttpMsg ToFile(string url, byte[] fromBody, Dictionary<string, string> parameters = null)
        {
            return To(url, fromBody, parameters);
        }

        private HttpMsg To(string url, byte[] fromBody, Dictionary<string, string> parameters)
        {
            string urldata = "";
            if (parameters != null)
            {
                foreach (var key in parameters.Keys)
                {
                    urldata += (key + "=" + parameters[key] + "&");
                }
                if (url.IndexOf("?") == -1)
                {
                    url = url + "?" + urldata;
                }
                else
                {
                    url = url + "&" + urldata;
                }
            }
            

            HttpMsg msg = new HttpMsg();
          
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = method;
            request.Accept = accept;
            request.ContentType = "application/x-www-form-urlencoded";
           

            request.ContentLength = fromBody.Length;
            request.GetRequestStream().Write(fromBody, 0, fromBody.Length);
            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
                msg.IsOk = true;
            }
            catch (WebException ex)
            {
                response = (HttpWebResponse)ex.Response;
            }

            using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(strEncoding)))
            {
                msg.Text = reader.ReadToEnd();
            }

            return msg;
        }

    }

    /// <summary>
    /// 消息
    /// </summary>
    public class HttpMsg
    {
        private string responseText = "";

        public string Text
        {
            get { return responseText; }
            set { responseText = value; }
        }

        private bool isOk = false;

        public bool IsOk
        {
            get { return isOk; }
            set { isOk = value; }
        }
    }
}
